package com.strugglerz.web.base.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * 序列化所用工具类
 * by hkNaruto 2012-02-23
 */
public class SerializeTool {

    /** logger */
    private static final Logger logger = LoggerFactory.getLogger(SerializeTool.class);

    /*
     * 实现十六进制字符串转化为二进制数组! by hkNaruto 2012-02-23 http://franksinger.iteye.com/blog/614540
     */
    public static byte[] hexStringToByteArray(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /*
     * 实现二进制数组转化为十六进制字符串 by hkNaruto 2012-02-23 参考 http://franksinger.iteye.com/blog/614540
     * @param byteArray 输入二进制数组
     */
    public static String byteArrayToHexString(byte[] byteArray) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (byteArray == null || byteArray.length <= 0) {
            return null;
        }
        for (int i = 0; i < byteArray.length; i++) {
            int v = byteArray[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /*
     * 将可序列化对象转换成二进制数组! by hkNaruto 2012-02-23
     */
    public static byte[] objectToByteArray(Object object) {
        byte[] byteArray = null;
        try {
            ByteArrayOutputStream bStream = new ByteArrayOutputStream();
            ObjectOutputStream outStream;
            outStream = new ObjectOutputStream(bStream);
            outStream.writeObject(object);
            outStream.close();
            byteArray = bStream.toByteArray();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return byteArray;
    }

    /*
     * 将序列化的二进制数组转化成Object对象实例 by hkNaruto 2012-02-23
     */
    public static Object byteArrayToObject(byte[] byteArray) {
        Object object = null;

        try {
            if (byteArray != null && byteArray.length != 0) {
                ByteArrayInputStream bStream = new ByteArrayInputStream(byteArray);
                ObjectInputStream inStream = new ObjectInputStream(bStream);
                object = inStream.readObject();
                inStream.close();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return object;
    }

    /*
     * 快捷函数 实现 HexStringToByteArray -> ByteArrayToObject add by hkNaruto 2012-02-23
     */
    public static Object hexStringToObject(String hexString) {
        Object object = null;
        byte[] byteArray = hexStringToByteArray(hexString);
        object = byteArrayToObject(byteArray);
        return object;
    }

    /*
     * 快捷函数 实现ObjectToByteArray -> ByteArrayToHexString add by hkNaruto 2012-02-23
     */
    public static String objectToHexString(Object object) {
        String hexString = null;
        byte[] byteArray = objectToByteArray(object);
        hexString = byteArrayToHexString(byteArray);
        return hexString;
    }

}
